import { defineStore } from "pinia";
import { ref, reactive } from "vue";

export const useUserStore = defineStore("user", () => {
  // 存储路由菜单
  const userInfo = reactive<UserInfo>({
    name: "admin",
    avatar: "https://wx1.sinaimg.cn/mw690/006GUdCXly1hsmmod74asj31hc1hcdnl.jpg",
    token: "",
  });
  // 存储个人信息
  const personInfo = reactive<PersonInfo>({
    name: "admin",
    adv: "",
    description: "人生在世能有几何",
    tagList: ["帅哥", "程序员", "二次元"] as string[],
    sex: "男",
  });

  const getUserInfo = () => {
    const token = localStorage.getItem("token");
    if (token) {
      userInfo.token = token;
      // 这里可以添加获取用户信息的接口调用
      userInfo.name = "admin";
      userInfo.avatar = "https://wx1.sinaimg.cn/mw690/006GUdCXly1hsmmod74asj31hc1hcdnl.jpg";

      // 设置个人信息
      personInfo.name = "admin";
      personInfo.description = "人生在世能有几何";
      personInfo.tagList = ["帅哥", "程序员", "二次元"];
      personInfo.sex = "男";
    }
  };

  const logout = () => {
    localStorage.removeItem("token");
    $reset();
  };

  const $reset = () => {
    Object.assign(userInfo, {
      name: "",
      avatar: "",
      token: null,
    });
    Object.assign(personInfo, {
      name: "",
      adv: "",
      description: "",
      tagList: [],
      sex: "",
    });
  };

  return {
    userInfo,
    personInfo,
    getUserInfo,
    logout,
    $reset,
  };
});

interface UserInfo {
  name: string;
  avatar: string;
  token: string | null;
}

interface PersonInfo {
  name: string;
  adv: string;
  description: string;
  tagList: string[];
  sex: string;
}
